package com.lipeng.controller;

import com.lipeng.bean.Club;
import com.lipeng.bean.Game;
import com.lipeng.service.GameService;
import com.lipeng.utils.CopyPic;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Created by dllo on 18/3/8.
 */
@Controller
public class GameController {
    @Resource
    private GameService gameService;

    @RequestMapping(value = "gamelist")
    public String gameList() {
        return "gamelist";
    }

    @RequestMapping(value = "gamesearch")
    public String gameSearch() {
        return "gamesearch";
    }

    @RequestMapping(value = "selectgame")
    @ResponseBody
    public Game selectGameById(@RequestParam("gid") Integer gid) {
        return gameService.selectGameById(gid);
    }

    @ResponseBody
    @RequestMapping("allgame")
    public List<Game> selectAllGame() {
        return gameService.selectAllGame();
    }

    @RequestMapping(value = "addgame")
    public String addGame(MultipartFile file, HttpServletRequest request, Model model,@RequestParam("gid")Integer gid) throws IOException {
        String name = request.getParameter("name");
        String intro = request.getParameter("intro");
        String place = request.getParameter("place");
        int price = Integer.valueOf(request.getParameter("price"));
        String time = request.getParameter("time");

        System.out.println(gid);

        Game game = new Game();
        game.setGname(name);
        game.setGintro(intro);
        game.setGplace(place);
        game.setGprice(price);
        game.setGtime(time);
        game.setFile(file);
        //保存数据库的路径
        String sqlPath = null;
        //定义文件保存的本地路径
        String localPath = "/Users/dllo/Desktop/frame/GameApp/src/main/webapp/images/";
        String targetPath = "/Users/dllo/Desktop/frame/GameApp/target/GameApp/images/";
        //定义 文件名
        String filename = null;
        if (game.getFile()!=null) {
            //生成uuid作为文件名称
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            //获得文件类型（可以判断如果不是图片，禁止上传）
            String contentType = game.getFile().getContentType();
            //获得文件后缀名
            String suffixName = contentType.substring(contentType.indexOf("/") + 1);
            //得到 文件名
            filename = uuid + "." + suffixName;
            //文件保存路径
            game.getFile().transferTo(new File(localPath + filename));
            CopyPic.copyFile(localPath+filename,targetPath+filename);
            //把图片的相对路径保存至数据库
            sqlPath = "/images/" + filename;
        }else {
            sqlPath = gameService.selectGameById(gid).getGpic();
        }

        game.setGpic(sqlPath);
        if (gid > 0 ) {
            request.getSession().setAttribute("name", name);
            request.getSession().setAttribute("intro", intro);
            request.getSession().setAttribute("place", place);
            request.getSession().setAttribute("price", price);
            request.getSession().setAttribute("time", time);
            request.getSession().setAttribute("pic", sqlPath);
            return "redirect:updategame?gid=" + gid;
        }
        int result = gameService.insertGame(game);
        model.addAttribute("game", game);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:gamelist";
    }


    @RequestMapping(value = "updategame")
    public String updateGame(HttpServletRequest request, Model model) throws IOException {
        String name = (String) request.getSession().getAttribute("name");
        String intro = (String) request.getSession().getAttribute("intro");
        String place = (String) request.getSession().getAttribute("place");
        int price = (int) request.getSession().getAttribute("price");
        String time = (String) request.getSession().getAttribute("time");
        String pic = (String) request.getSession().getAttribute("pic");
        request.getSession().removeAttribute("name");
        request.getSession().removeAttribute("intro");
        request.getSession().removeAttribute("place");
        request.getSession().removeAttribute("price");
        request.getSession().removeAttribute("time");
        request.getSession().removeAttribute("pic");

        int gid = Integer.valueOf(request.getParameter("gid"));

        Game game = new Game();
        game.setGid(gid);
        game.setGname(name);
        game.setGintro(intro);
        game.setGplace(place);
        game.setGprice(price);
        game.setGtime(time);
        game.setGpic(pic);

        int result = gameService.updateGame(game);
        model.addAttribute("game", game);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:gamelist";
    }

    @RequestMapping(value = "delgame")
    public String deleteGameById(HttpServletRequest request, @RequestParam("gid") Integer gid) {
        int result = gameService.deleteGameById(gid);
        if (result > 0) {
            request.getSession().setAttribute("success", "success");
        } else {
            request.getSession().setAttribute("fail", "fail");
        }
        return "redirect:gamelist";
    }

    @ResponseBody
    @RequestMapping(value = "searchgamename")
    public List<Game> searchname(@Param("name")String name){
        List<Game> gameList = gameService.selectGameByName(name);
        return gameList;
    }
}
